﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AutoMapper;

using Revis.VClinicNet.Core.Domain;
using Revis.VClinicNet.Core.Domain.Repository;
using Revis.VClinicNet.App.Internal.Dto;
using Revis.VClinicNet.App.Internal.Dto.Validation;

namespace Revis.VClinicNet.App.Internal.ServiceImpl
{
    public class WorkTimeService : IWorkTimeService
    {
        private IWorkTimeRepository _workTimeRepository = null;

        public WorkTimeService(IWorkTimeRepository iwtr)
        {
            _workTimeRepository = iwtr;
        }

        public long Edit(WorkTimeDto workTimeDto)
        {
            Validation.For<WorkTimeDto>().Using<WorkTimeDtoRules>().Check(workTimeDto);

            WorkTime workTime = _workTimeRepository.Find(workTimeDto.Id);
            if(workTime==null) {
                throw new ClinicException("Nie znaleziono takiego czasu pracy");
            }

            workTime.Change(
                new Time(workTimeDto.BeginHour, workTimeDto.BeginMinute),
                new Time(workTimeDto.EndHour, workTimeDto.EndMinute),
                workTimeDto.Room,
                workTimeDto.IsWorking);

            _workTimeRepository.Store(workTime);

            return workTime.Id;
        }

        public WorkTimeDto Find(long id)
        {
            WorkTime workTime = _workTimeRepository.Find(id);
            if (workTime == null) {
                throw new ClinicException("Nie znaleziono takiego czasu pracy");
            }
            WorkTimeDto dto = Mapper.Map<WorkTime, WorkTimeDto>(workTime);
            return dto;
        }
    }
}
